/**
 * 收藏功能JS文件
 * 
 * @author		布衣才子
 * @date		2012-10-01
 * @email		work.jerryliu@gmail.com
 * @qq			394969553
 * @version		v1.0
 * @copyright	copyright 2012-2014	YeeYi.com All Rights Reserved	
 */


/**
 * 
 * 服务器访问地址
 */
var forumHost = "http://www.yeeyi.com/bbs/forum.php?jsoncallback=?&mod=mobile";

/**
 * 用于分页
 */
var forumPage = 1;
var b64 = new Base64();

/**
 * 网络访问函数
 * @param {Object} url	请求的网络地址
 * @param {Object} callback	回调函数
 */
function xmlHttp(url,callback){
	if(url == ''){
		uexWindow.alert('参数错误','请求地址不能为空！','返回');
	}else{
		var platform = getLocalStorge('platform');
		url = url + "&pf="+platform;
		$.getJSON(url,callback);
	}
}

/**
 * 获取收藏列表
 */
function  getFavoriteList(){
	uexWindow.toast("1","5","数据加载中…","0");
	var uid = getLocalStorge('uid');
	if(!uid){
		uexWindow.closeToast();
		uexWindow.alert('提示','获取失败，请先登录！','返回');
	}
	
	forumPage = 1;
	var url = forumHost + '&ac=favoritelist&uid='+uid;
	xmlHttp(url,showList);
}

/**
 * 获取收藏列表的回调函数
 * @param {Object} items
 */
function showList(items){
	var outNewsObj = $("#forum_list");
	outNewsObj.html('');
	var j = 0;
	for(var i in items){
		j = j + 1;
		var item = items[i];
		var tr = '';
		var trHeader = '<div ontouchstart="zy_touch(\'btn-newact\')" class="forumList" onclick="goThreadWindow('+item.id+')"><div class="ub-f1 ub ub-ver">';
		var trTitle = '<div class="title">'+b64.decode(item.subject)+'</div>';
		var trOtherInfo = '<div class="otherInfo2"><span>收藏时间：'+b64.decode(item.dateline)+'</span> <span style="margin-left:1em;"> 作者： '+b64.decode(item.author)+'</span></div>';
		var trFooter = '</div></div>';
		tr = trHeader+trTitle+trOtherInfo+trFooter;
		outNewsObj.append(tr);
	}
	uexWindow.closeToast();
	
	if(j == 0){
			var tr = '';
			var trHeader = '<div ontouchstart="zy_touch(\'btn-newact\')" class="forumList"><div class="ub-f1 ub ub-ver">';
			var trTitle = '<div class="title">你的收藏夹里面还没有帖子哟！</div>';
			var trOtherInfo = '<div class="otherInfo2"><span></span> <span style="margin-left:1em;"></span></div>';
			var trFooter = '</div></div>';
			tr = trHeader+trTitle+trOtherInfo+trFooter;
			outNewsObj.append(tr);
	}
	
	if(j == 10){
		$('.footer').removeClass('footerHide');
	}
}

/**
 * 显示下一页收藏列表
 * @param {Object} items
 */
function showMoreFavorite(items){
	var outThreadObj = $("#forum_list");
	var j = 0;
	for(var i in items){
		j = j + 1;
		
		var item = items[i];
		var tr = '';
		var trHeader = '<div ontouchstart="zy_touch(\'btn-newact\')" class="forumList" onclick="goThreadWindow('+item.id+')"><div class="ub-f1 ub ub-ver">';
		var trTitle = '<div class="title">'+b64.decode(item.subject)+'</div>';
		var trOtherInfo = '<div class="otherInfo2"><span>收藏时间：'+b64.decode(item.dateline)+'</span> <span style="margin-left:1em;">作者： '+b64.decode(item.author)+'</span></div>';
		var trFooter = '</div></div>';
		tr = trHeader+trTitle+trOtherInfo+trFooter;
		outThreadObj.append(tr);
	}
	uexWindow.closeToast();
	if(j == 10){
		$("#nextpage").text("查看下10条");
	}else{
		$("#nextpage").text("已经全部显示");
	}
	
}

/**
 * 获取下一页收藏列表
 */
function getMoreFavorite(){
	var uid = getLocalStorge('uid');
	forumPage = forumPage + 1;
	var url = forumHost + '&ac=favoritelist&uid='+uid+'&page='+forumPage;
	
	$("#nextpage").text("数据加载中……");
	xmlHttp(url,showMoreFavorite);
}

/**
 * 帖子跳转函数
 * @param {Object} tid
 */
function goThreadWindow(tid){
	if(tid == ''){
		uexWindow.alert("ERROR","未指定帖子ID,无法加载帖子内容！","返回");
	}else{
		localStorage.setItem("tid",tid);
		var threadUrl = "view_self_thread.html";
		uexWindow.open("threadWindow",0,threadUrl,2,"","",4);
	}
}

/**
 * 关闭窗口
 */
function closeWindow(){
    var closeString = "uexWindow.close(-1);";
    uexWindow.evaluateScript('', '0', closeString);
}

/**
 * HTML5 lcoalStage.getItem 的封装函数
 * @param {Object} key
 */
function getLocalStorge(key){
	var item = localStorage.getItem(key);
	if(item=='' || item==null || typeof(item)=="undefined"){
		return false;
	}else{
		return item;
	}
}